import { _decorator, Component, Node, Animation, Label, find, tween, v3 } from 'cc';
import api from '../api'
import useStore from "../store";
import arithmetic from '../utils/arithmetic'
import CryptoJS from "../utils/crypto-js.min.js";

import { ProgressMgr } from './ProgressMgr'
import { scoreMgr } from './scoreMgr'
import { overGame } from './overGame'
import dicScore from "./dic-score";
import { Constants } from "../data/Constants";
import { UIManager } from "../utils/UIManager";
import { AudioManager } from "../audio/AudioManager";
const { ccclass, property } = _decorator;

enum gameStatus {
    start = 'start',
    videoEnd = 'videoEnd'
}

@ccclass('TopicManger')
export class TopicManger extends Component {

    public static timuNodeRef: Node = null // 题目

    private Arithmetic = new arithmetic({})

    private quizLen: number = 200 // 每次请求练习题数量
    public static quiz = []   // 题目列表
    public static quizIndex: number = 0 // 当前Index
    public static btnStatus = false

    public static isShare = false // 是否已经分享过
    public static curScore = 0
    public static combo = -1
    public static comboNum = 0
    public static comboImgUrl = ''

    onLoad() {
        console.log("游戏页面")
        this.getQuizList()
    }

    start() {
        console.log("游戏页面start")
        TopicManger.timuNodeRef = find("Canvas/timu")
        this.resetTopic()
        this.readyGo()
    }

    getQuizList() {
        // 生成题目列表
        TopicManger.quizIndex = 0
        TopicManger.quiz = this.Arithmetic.workbook(this.quizLen)

    }

    resetTopic() {
        console.log("生成题目")
        if (TopicManger.quizIndex < TopicManger.quiz.length) {
            this.topicRendering()
        } else {
            this.getQuizList()
            this.topicRendering()
        }

    }

    readyGo() {
        console.log(112312)
        UIManager.showDialog(Constants.UIPage.readyGo);
        AudioManager.playSound(Constants.AudioSource.readyGo);
        setTimeout(() => {
            // 预制体如何重置动画
            UIManager.hideDialog(Constants.UIPage.readyGo);
            this.resetGameData()
            this.startGame()
        }, 1200)
    }


    resetGameData() {
        TopicManger.curScore = 0
        TopicManger.combo = -1
        TopicManger.comboNum = 0
        TopicManger.comboImgUrl = ''
        overGame.isRevive = false
        scoreMgr.prototype.hideFenshuModal()
    }


    startGame() {
        console.log('startGame开始游戏')
        ProgressMgr.prototype.resetProgress() // 重置时间
        TopicManger.prototype.resetTopic() // 重置题目
    }

    topicRendering() {
        // 生成题目节点更新
        TopicManger.timuNodeRef.children[0].getComponent(Label).string = TopicManger.quiz[TopicManger.quizIndex].fu1
        TopicManger.timuNodeRef.children[1].getComponent(Label).string = TopicManger.quiz[TopicManger.quizIndex].o
        TopicManger.timuNodeRef.children[2].getComponent(Label).string = TopicManger.quiz[TopicManger.quizIndex].fu2
        TopicManger.timuNodeRef.children[3].getComponent(Label).string = "="
        TopicManger.timuNodeRef.children[4].getComponent(Label).string = TopicManger.quiz[TopicManger.quizIndex].mockAnswer
    }


    correctAnswer() {
        // console.log('答对了')
        if (ProgressMgr.isCombo) {
            TopicManger.combo += 1
            console.log(TopicManger.combo, 'TopicManger.combo')
            const levelInfo: any = dicScore.getLevel(TopicManger.combo)
            TopicManger.comboNum = levelInfo.score
            TopicManger.curScore += levelInfo.score
            TopicManger.comboImgUrl = levelInfo.url
            console.log(121, levelInfo)
        } else {
            TopicManger.combo = 0
            TopicManger.curScore += 1
            TopicManger.comboNum = 1
            TopicManger.comboImgUrl = ''
        }
        scoreMgr.prototype.showFenshuModal()
        TopicManger.quizIndex = TopicManger.quizIndex + 1
        this.startGame()
    }


    wrongAnswer() {
        // console.log('答错了')
        TopicManger.combo = -1
        this.overModalShowDialog()
    }

    // 展示弹窗
    overModalShowDialog() {
        UIManager.showDialog(Constants.UIPage.overModalDialog);
        this.updaAddScoreRecord()
    }

    // 更新分数
    updaAddScoreRecord() {
        const key = '@$-2022!-XR&$#@?-he&!xyz'
        if (useStore().userStore.userInfo.maxScore && TopicManger.curScore > useStore().userStore.userInfo.maxScore) {
            console.log('新纪录')
            useStore().userStore.setUserInfo({ maxScore: TopicManger.curScore })
        }
        if (Number(TopicManger.curScore) === 0) return
        const uploadTime = Date.now()
        const hash = CryptoJS.HmacSHA256(`${useStore().userStore.userInfo.id}-${useStore().userStore.appId}-${TopicManger.curScore}-${uploadTime}`, key)
        api.game.addScoreRecord({
            score: TopicManger.curScore,
            uploadTime,
            appId: useStore().userStore.appId,
            sign: hash.toString(CryptoJS.enc.Base64)
        })
    }

    update(deltaTime: number) {

    }
}

